home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / c / zoo / io.c < prev    next >
C/C++ Source or Header  |  1980-01-02  |  3KB  |  163 lines

  1. /*$Source: /usr/home/dhesi/zoo/RCS/io.c,v $*/
  2. /*$Id: io.c,v 1.14 91/07/09 01:39:54 dhesi Exp $*/
  3. /***********************************************************
  4.     io.c -- input/output
  5.  
  6. Adapted from "ar" archiver written by Haruhiko Okumura.
  7. ***********************************************************/
  8.  
  9. #include "options.h"
  10. #include "zoo.h"
  11. #include "ar.h"
  12. #include "lzh.h"
  13. #include "zooio.h"   /* for NULLFILE */
  14. #include "portable.h"
  15. #include "zoofns.h" /*OIS*/
  16.  
  17. #ifdef ANSI_HDRS
  18. # include <stdlib.h>
  19. # include <string.h>
  20. #endif
  21.  
  22. #ifdef _DCC
  23. #undef putc
  24. #define putc fputc
  25. #endif
  26.  
  27. #include "errors.i"
  28.  
  29. #define JUST_LZH        /* for stand-alone compression */
  30.  
  31. #if 0
  32. # define CRCPOLY    0xA001    /* ANSI CRC-16 */ /* CCITT: 0x8408 */
  33. # define UPDATE_CRC(c) \
  34.     crc = crctable[(crc ^ (c)) & 0xFF] ^ (crc >> CHAR_BIT)
  35. static ushort crctable[UCHAR_MAX + 1];
  36. t_uint16 crc;
  37. #endif
  38.  
  39. extern FILE *arcfile, *lzh_outfile;
  40. t_uint16 bitbuf;
  41. int unpackable;
  42.  
  43. ulong compsize, origsize;
  44.  
  45. /*static*/ uint  subbitbuf;
  46. /*static*/ int   bitcount;
  47.  
  48. #if 0
  49. void make_crctable()
  50. {
  51.     uint i, j, r;
  52.  
  53.     for (i = 0; i <= UCHAR_MAX; i++) {
  54.         r = i;
  55.         for (j = 0; j < CHAR_BIT; j++)
  56.             if (r & 1) r = (r >> 1) ^ CRCPOLY;
  57.             else          r >>= 1;
  58.         crctable[i] = r;
  59.     }
  60. }
  61. #endif
  62.  
  63. void fillbuf(n)  /* Shift bitbuf n bits left, read n bits */
  64. int n;
  65. {
  66.     bitbuf <<= n;
  67.     while (n > bitcount) {
  68.         bitbuf |= subbitbuf << (n -= bitcount);
  69. #ifdef JUST_LZH
  70.         if (feof(arcfile))
  71.             subbitbuf = 0;
  72.         else
  73.             subbitbuf = (uchar) zgetc(arcfile);
  74. #else
  75.         if (compsize != 0) {
  76.             compsize--;  subbitbuf = (uchar) zgetc(arcfile);
  77.         } else subbitbuf = 0;
  78. #endif /* JUST_LZH */
  79.         bitcount = CHAR_BIT;
  80.     }
  81.     bitbuf |= subbitbuf >> (bitcount -= n);
  82. }
  83.  
  84. uint getbits(n)
  85. int n;
  86. {
  87.     uint x;
  88.  
  89.     x = bitbuf >> (BITBUFSIZ - n);  fillbuf(n);
  90.     return x;
  91. }
  92.  
  93. void putbits(n, x)  /* Write rightmost n bits of x */
  94. int n;
  95. uint x;
  96. {
  97.     if (n < bitcount) {
  98.         subbitbuf |= x << (bitcount -= n);
  99.     } else {
  100. #ifdef JUST_LZH
  101.         (void) putc((int) (subbitbuf | (x >> (n -= bitcount))), lzh_outfile);
  102.         compsize++;
  103. #else
  104.         if (compsize < origsize) {
  105.             (void) zputc((int) (subbitbuf | (x >> (n -= bitcount))), lzh_outfile);
  106.             compsize++;
  107.         } else unpackable = 1;
  108. #endif /* JUST_LZH */
  109.  
  110.         if (n < CHAR_BIT) {
  111.             subbitbuf = x << (bitcount = CHAR_BIT - n);
  112.         } else {
  113. #ifdef JUST_LZH
  114.             (void) putc((int) (x >> (n - CHAR_BIT)), lzh_outfile);
  115.             compsize++;
  116. #else
  117.             if (compsize < origsize) {
  118.                 (void) zputc((int) (x >> (n - CHAR_BIT)), lzh_outfile);
  119.                 compsize++;
  120.             } else unpackable = 1;
  121. #endif /* JUST_LZH */
  122.             subbitbuf = x << (bitcount = 2 * CHAR_BIT - n);
  123.         }
  124.     }
  125. }
  126.  
  127. /*extern void addbfcrc(); OIS*/
  128.  
  129. int fread_crc(p, n, f)
  130. uchar *p;
  131. int n;
  132. FILE *f;
  133. {
  134.     int i;
  135.  
  136.     i = n = fread((char *) p, 1, n, f);  origsize += n;
  137.     addbfcrc((char *)p, i);
  138.     return n;
  139. }
  140.  
  141. void fwrite_crc(p, n, f)
  142. uchar *p;
  143. int n;
  144. FILE *f;
  145. {
  146.     if (f != NULLFILE) {
  147.         if (fwrite((char *) p, 1, n, f) < n)
  148.             prterror('f', disk_full);
  149.     }
  150.     addbfcrc((char *)p, n);
  151. }
  152.  
  153. void init_getbits()
  154. {
  155.     bitbuf = 0;  subbitbuf = 0;  bitcount = 0;
  156.     fillbuf(BITBUFSIZ);
  157. }
  158.  
  159. void init_putbits()
  160. {
  161.     bitcount = CHAR_BIT;  subbitbuf = 0;
  162. }
  163.